﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace NeoReflow
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
    

        protected override void OnStartup(StartupEventArgs e)
        {
            RegisterEvents();
            base.OnStartup(e);
        }

        private void RegisterEvents()
        {
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;

            this.DispatcherUnhandledException += App_DispatcherUnhandledException;

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                var exception = e.ExceptionObject as Exception;
                if (exception != null)
                {
                    HandleException(exception);
                }
            }
            catch (Exception ex)
            {

                HandleException(ex);
            }
            finally
            {

            }
        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            try
            {
                HandleException(e.Exception);
            }
            catch (Exception ex)
            {

                HandleException(ex);
            }
            finally
            {
                e.Handled = true;
            }
        }

        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {
            try
            {
                var exception = e.Exception as Exception;
                if (exception != null)
                {
                    HandleException(exception);
                }
            }
            catch (Exception ex)
            {

                HandleException(ex);
            }
            finally
            {
                e.SetObserved();
            }
        }

        private static void HandleException(Exception ex)
        {
            //NewSelectiveSoldering.ExtendFunction.ExceptionHelper.ExceptionLogger.LogWrite(ex);
        }

        private static void SetStartupUri()
        {
            string machineType = System.Configuration.ConfigurationSettings.AppSettings["MachineType"].ToString();

            //machineType = "";

            if (machineType == "JTR")
            {
                //NewSelectiveSoldering.General.Contexts.Context.MachineType = "RotaryTable";
                Application.Current.StartupUri = new System.Uri("UI\\MainForm\\JTRMainWindow.xaml", System.UriKind.Relative);
            }
            else
            {
                Application.Current.StartupUri = new System.Uri("UI\\MainForm\\JTRMainWindow.xaml", System.UriKind.Relative);

            }

        }

        [STAThread]
        public static void Main()
        {
            int count = 0;

            App app = new App();
            app.InitializeComponent();
            SetStartupUri();
            //app.DispatcherUnhandledException += app_DispatcherUnhandledException;

            try
            {
                app.Run();


            }
            catch (Exception ex)
            {
                HandleException(ex);
                MessageBox.Show(ex.StackTrace, ex.Message);

            }




        }

        static void app_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            HandleException(e.Exception);
            MessageBox.Show(e.Exception.StackTrace, e.Exception.Message);
        }
    }
}
